#!/bin/bash

echo "+++++++++++++++++++++++ $(cd `dirname $0`; pwd)/$(basename $0) +++++++++++++++++++++++++"

# 当前脚本所在目录
v_d_current=$(cd `dirname $0`; pwd)
# 当前脚本的配置信息
v_f_settings=${v_d_current}/settings
v_f_server_config=${v_d_current}/tomcat-server.xml
v_f_service_config=${v_d_current}/tomcat-service

# 检查配置文件是否存在
if [ ! -f ${v_f_settings} ]; then
    echo "settings file does not exist : $v_f_settings"
    exit -1
fi

# 加载配置信息
source ${g_f_util_properties} "load" ${v_f_settings}

# 检查安装目录是否存在
if [ ! -n "$install_dir" ] || [ ! -d ${install_dir} ]; then
    echo "installation directory does not exist : $install_dir"
    exit -1
fi

# 检查安装包是否存在
if [ ! -n "$install_file" ] || [ ! -f ${install_file} ]; then
    echo "installation file does not exist : $install_file"
    exit -1
fi

# 检查服务安装包是否存在
if [ ! -n "$server_file" ] || [ ! -f ${server_file} ]; then
    echo "installation file does not exist : $server_file"
    exit -1
fi

# apache-tomcat-7.0.62.tar.gz -> /opt/jenkins
tar -zxf ${server_file} -C ${install_dir}
# tomcat-server.xml -> tomcat/conf/server.xml
cd ${install_dir}/*tomcat*
v_d_server=`pwd`
\cp ${v_d_current}/tomcat-server.xml ${v_d_server}/conf/server.xml
# find tomcat/webapps
v_d_deploy=${v_d_server}/webapps
if [ ! -d ${v_d_deploy} ]; then
    echo "deploy directory does not exist : $v_d_deploy"
    exit -1
fi

# war -> tomcat/webapps/
\cp ${install_file} ${v_d_deploy}
mv -f ${v_d_deploy}/dubbo-admin-*.war ${v_d_deploy}/dubbo-admin.war

# 设置开机启动
v_service="dubbo-admin"
v_f_service="/etc/init.d/$v_service"
touch "$v_f_service"
${g_f_util_replace} -s "$v_f_service_config" -t "$v_f_service" HOME=${v_d_server}
chmod 755 "$v_f_service"
chown root "$v_f_service"
chkconfig --add "$v_service"
chkconfig --levels 345 "$v_service" on

source /etc/profile
chkconfig --list | grep "$v_service"
exit -1